class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {
        auto max_it=max_element(piles.begin(),piles.end());
        int left=0;
        int right=*max_it;
        while(left+1<right){
            int mid=left+(right-left)/2;
            int sum=0;
            for(int i=0;i<piles.size();i++){
                if(piles[i]<=mid){
                    sum++;
                    continue;
                }
                sum += (piles[i] + mid - 1) / mid;
                //提前退出
                if (sum > h) {
                    break;
                }
                // int tem=piles[i];
                // while(tem>0){
                //     tem-=mid;
                //     sum++;
                // }
            }
            if(sum<=h)
                right=mid;
            else 
                left=mid;
        }
        return right;
    }
};
